package com.kgc.controller;

import com.alibaba.fastjson.JSONObject;
import com.kgc.entity.GoodSort;
import com.kgc.entity.GoodsDetail;
import com.kgc.service.GoodsDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class GoodsDetailController {
    @Autowired
    private GoodsDetailService service;
    @RequestMapping("/list")
    @ResponseBody
    public String list(String name,int currentPage){
        System.out.println(name+""+currentPage);
        List<GoodsDetail> list = service.select(name, currentPage);
        int totalCount = service.countGood(name);
        System.out.println(list);
        int totalPage = totalCount % 6 == 0 ? totalCount / 6 : totalCount / 6 + 1;
        Map<String,Object> restMap=new HashMap<>();
        restMap.put("list",list);
        restMap.put("totalCount",totalCount);
        restMap.put("totalPage",totalPage);
        return JSONObject.toJSONString(restMap);
    }
    @RequestMapping("/listsort")
    @ResponseBody
    public String goodsort(){
        List<GoodSort> listsort = service.selectGoodSort();

        return JSONObject.toJSONString(listsort);
    }
}
